﻿using System;
using System.Windows.Forms;
using agsXMPP;

namespace Lapd.PoC
{
    public partial class Main : Form
    {
        private XmppClientConnection xmppClientConnection;

        public Main()
        {
            InitializeComponent();

            xmppClientConnection = new XmppClientConnection();

            xmppClientConnection.OnLogin += new ObjectHandler(XmppCon_OnLogin);
            xmppClientConnection.OnClose += new ObjectHandler(XmppCon_OnClose);
            xmppClientConnection.OnAuthError += new XmppElementHandler(XmppCon_OnAuthError);
            xmppClientConnection.OnError += new ErrorHandler(XmppCon_OnError);
            xmppClientConnection.OnSocketError += new ErrorHandler(XmppCon_OnSocketError);
            xmppClientConnection.OnStreamError += new XmppElementHandler(XmppCon_OnStreamError);
        }

        private void XmppCon_OnStreamError(object sender, agsXMPP.Xml.Dom.Element e)
        {
            MessageBox.Show(String.Format("StreamError: {0}", e.ToString()));
        }

        private void XmppCon_OnSocketError(object sender, Exception ex)
        {
            MessageBox.Show(String.Format("SocketError: {0}", ex.Message));
        }

        private void XmppCon_OnError(object sender, Exception ex)
        {
            MessageBox.Show(String.Format("ConnectionError: {0}", ex.Message));
        }

        private void XmppCon_OnAuthError(object sender, agsXMPP.Xml.Dom.Element e)
        {
            MessageBox.Show(String.Format("AuthError: {0}", e.ToString()));
        }

        private void XmppCon_OnClose(object sender)
        {
            MessageBox.Show(String.Format("OnClose event"));
        }

        private void XmppCon_OnLogin(object sender)
        {
            if (InvokeRequired)
            {
                // Windows Forms are not Thread Safe, we need to invoke this :(
                // We're not in the UI thread, so we need to call BeginInvoke				
                BeginInvoke(new agsXMPP.ObjectHandler(XmppCon_OnLogin), new object[] { sender });
                return;
            }

            mitFileLogin.Enabled = false;
            stbMain.Text = "Online";
            Session.UserName = xmppClientConnection.Username;
            Session.Server = xmppClientConnection.Server;
        }

        private void mnuSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void mnuLogin_Click(object sender, EventArgs e)
        {
            LogOn logOn = new LogOn(xmppClientConnection);

            if (logOn.ShowDialog() == DialogResult.OK)
            {
                xmppClientConnection.Open();
            }
        }

        private void mitFileJoin_Click(object sender, EventArgs e)
        {
            JoinGroup joinGroup = new JoinGroup(xmppClientConnection);
            joinGroup.Show();
        }

        private void mitWriteOnTopic_Click(object sender, EventArgs e)
        {
            WriteOnSampleTopic writeOnSampleTopic = new WriteOnSampleTopic(xmppClientConnection);
            writeOnSampleTopic.Show();
        }

        private void mitListenFromTopic_Click(object sender, EventArgs e)
        {
            ListenFromTopic listenFromTopic = new ListenFromTopic(xmppClientConnection);
            listenFromTopic.Show();
        }

        private void mitNewTopic_Click(object sender, EventArgs e)
        {
            NewTopic newTopic = new NewTopic(xmppClientConnection);
            newTopic.Show();
        }

        private void mitWriteOnMessageTopic_Click(object sender, EventArgs e)
        {
            WriteOnMessageTopic writeOnMessageTopic = new WriteOnMessageTopic(xmppClientConnection);
            writeOnMessageTopic.Show();
        }
    }
}